package com.bw.park

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bw.lib_base.entity.NoticeBeanItem
import com.bw.lib_base.entity.ParkBeanItem
import com.bw.park.databinding.ItemParkBinding

class ParkAdapter: PagingDataAdapter<ParkBeanItem, ParkAdapter.ParkViewHolder>(diff) {
    private var mOnItemClickListener: OnItemClickListener? =null
    private var mOnItemLongClickListener: OnItemLongClickListener? =null
    companion object{
        val diff = object :ItemCallback<ParkBeanItem>(){
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem.id == newItem.id
            }
        }
    }
    class ParkViewHolder(view: View): ViewHolder(view) {
        val binding = DataBindingUtil.bind<ItemParkBinding>(view)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.bean = item

        holder.binding?.cwglSpaceStatus?.apply {
            when(item?.spaceStatus){
                "空闲" ->{
                    setBackgroundResource(R.drawable.shape_status)
                    setTextColor(Color.GREEN)
                }
                "已占用" ->{
                    setBackgroundResource(R.drawable.shape_status1)
                    setTextColor(Color.BLUE)
                }
                "维修中" ->{
                    setBackgroundResource(R.drawable.shape_status2)
                    setTextColor((holder.itemView.context.getColor(R.color.orange)))

                }
            }
        }

        holder.itemView.setOnClickListener {
            this.mOnItemClickListener?.onItemClick(position, item!!)
        }
        holder.itemView.setOnLongClickListener {
            this.mOnItemLongClickListener?.onItemLongClick(item!!)
            return@setOnLongClickListener true
        }

    }

    fun getParkId(){

    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder((view))
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        this.mOnItemClickListener = listener
    }
    fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }
    interface OnItemClickListener {
        fun onItemClick(position: Int, item: ParkBeanItem)
    }
    interface OnItemLongClickListener {
        fun onItemLongClick(item: ParkBeanItem): Boolean
    }
}